package gui.assiste;

import gui.utils.JCoolButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

public class PanelOutput extends JDialog implements ActionListener{

	public static String OK = "ok";
	public static String ANNULER = "annuler";
	public static String BROWSE = "browse";

	public static int PDF = 0;
	public static int IMAGE = 1;

	public  JTextField field;
	public  JComboBox format;
	public  ArrayList<String> results;
	private PanelOutput(int mode){

		setTitle("Images Generation");
		setModalityType(ModalityType.APPLICATION_MODAL);

		if (mode == IMAGE){
			Object[] o= {"svg","png","pdf"};
			format = new JComboBox(o);
		}
		else if (mode == PDF){
			Object[] o= {"tex"};
			format = new JComboBox(o);
			format.setEnabled(true);
		}


		field = new JTextField();
		field.setText(FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath());
		field.setMinimumSize(new Dimension(225,20));
		field.setMaximumSize(new Dimension(225,20));
		field.setPreferredSize(new Dimension(225,20));

		format.setMinimumSize(new Dimension(75,20));
		format.setMaximumSize(new Dimension(75,20));
		format.setPreferredSize(new Dimension(75,20));

		this.setSize(450,200);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(createComponent(),BorderLayout.CENTER);
		this.getContentPane().add(createBottom(),BorderLayout.SOUTH);
		this.setBackground(new Color(230,230,230));
		this.setVisible(true);
	}

	public JComponent createComponent(){


		Box hbox = Box.createHorizontalBox();
		hbox.add(Box.createHorizontalStrut(20));
		hbox.add(new JLabel("output : "));
		hbox.add(field);
		hbox.add(Box.createHorizontalStrut(20));
		hbox.add(createButton("Browse",BROWSE));
		hbox.add(Box.createHorizontalGlue());

		Box hbox2 = Box.createHorizontalBox();
		hbox2.add(Box.createHorizontalStrut(20));
		hbox2.add(new JLabel("Format : "));
		hbox2.add(format);
		hbox2.add(Box.createHorizontalGlue());

		Box box = Box.createVerticalBox();
		box.add(Box.createVerticalStrut(20));
		box.add(createTitle("Output Path"));
		box.add(Box.createVerticalStrut(20));
		box.add(hbox);
		box.add(Box.createVerticalStrut(20));
		box.add(hbox2);
		box.add(Box.createVerticalStrut(20));

		return box;
	}

	private JComponent createTitle(String title){
		JPanel panelLabel = new JPanel(new FlowLayout());
		JLabel label = new JLabel(title);
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(250,25));
		panelLabel.setMaximumSize(new Dimension(250,25));
		return panelLabel;
	}

	private JButton createButton(String name,String action){
		JButton b = new JCoolButton(name,new Color(75,75,75), new Color(0,101,135));
		b.setActionCommand(action);
		b.addActionListener(this);
		return b;
	}
	private JComponent createBottom(){
		Box mainbox = Box.createHorizontalBox();
		mainbox.add(Box.createHorizontalGlue());
		mainbox.add(createButton("ok",OK));
		mainbox.add(Box.createHorizontalStrut(20));
		mainbox.add(createButton("cancel",ANNULER));
		mainbox.add(Box.createHorizontalGlue());
		return mainbox;

	}

	public ArrayList<String> getResults(){return results;}

	public static ArrayList<String> getArguments(int mode){
		PanelOutput out = new  PanelOutput(mode);
		if (!out.getResults().get(0).endsWith("/")) out.getResults().set(0,out.getResults().get(0)+"/");
		return out.getResults();
	}

	public void actionPerformed(ActionEvent a) {
		// TODO Auto-generated method stub
		String action = a.getActionCommand();
		if (action.equals(BROWSE)){
			JFileChooser f = new JFileChooser();
			f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			f.showSaveDialog(new JFrame());
			field.setText(f.getSelectedFile().getAbsolutePath());
		}
		if (action.equals(ANNULER)){
			dispose();
		}
		if (action.equals(OK)){
			File file = new File(field.getText());
			if (file.exists()){
				results = new ArrayList<String>();
				results.add(field.getText());
				results.add((String) format.getSelectedItem());
			}
			else{JOptionPane.showMessageDialog(new JFrame(),"Dossier inconnue");}

			dispose();
		}

	}
}
